package com.lqe.study.Session;

import com.lqe.study.Entity.Person;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;

@WebServlet("/SessionDemo01")
public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        HttpSession session = req.getSession();

        Person person1 = new Person("刘庆恩",25,1);
        Person person2 = new Person("刘庆恩",25,1);
        Person person3= new Person("刘庆恩",25,1);
        session.setAttribute("person1",person1);
        session.setAttribute("person2",person2);
        session.setAttribute("person3",person3);


        String id = session.getId();

        if(session.isNew()){
            resp.getWriter().println("session创建成功，sessionID为："+id);
        }else{
            resp.getWriter().println("session已经存在了！");
        }

       /*
        Seession创建的时候存到Cookie中去了
        Cookie cookie = new Cookie("JSESSIONID", id);
        resp.addCookie(cookie);
        */


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
